<template>
  <div id="detail">
    <detail-nav-bar @titleClick="titleClick" class="detail-nav" ref="nav" />
    <scroll
      class="content"
      ref="scroll"
      @scroll="contentScroll"
      :probe-type="3"
    >
      <detail-swiper :top-images="topImages" />
      <detail-base-info :goods="goods" />
      <detail-shop-info :shop="shop" />
      <detail-goods-info
        :detail-info="detailInfo"
        @detailImageLoad="detailImageLoad"
      />
      <detail-param-info ref="params" :param-info="paramInfo" />
      <detail-comment-info ref="comment" :comment-info="commentInfo" />
      <goods-list ref="recommend" :goods="recommends" />
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop" />
    <detail-bottom-bar @addCart="addToCart" />
  </div>
</template>

<script>
import DetailNavBar from "./compons/DetailNavBar"; //顶部导航
import DetailSwiper from "./compons/DetailSwiper"; //轮播图
import DetailBaseInfo from "./compons/DetailBaseInfo"; //基本信息
import DetailShopInfo from "./compons/DetailShopInfo"; //店铺的基本信息

import DetailGoodsInfo from "./compons/DetailGoodsInfo"; //商品图片展示
import DetailParamInfo from "./compons/DetailParamInfo"; //商品的参数信息
import DetailCommentInfo from "./compons/DetailCommentInfo"; //用户评论信息
import DetailBottomBar from "./compons/DetailBottomBar"; //底部导航

import Scroll from "components/common/scroll/Scroll";
import GoodsList from "components/content/goods/GoodsList";

import { debounce } from "common/utils";
import { itemListenerMixin, backTopMixin } from "common/mixin";

import { mapActions } from "vuex";

import {
  getDetail,
  Goods,
  Shop,
  GoodsParam,
  getRecommend
} from "network/detail";

export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar
  },
  mixins: [itemListenerMixin, backTopMixin], //混合到下面的mounted
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null, //用于方法3
      currentIndex: 0
    };
  },

  created() {
    //获取iid的值
    this.iid = this.$route.params.iid;

    //根据iid请求详情页数据
    getDetail(this.iid).then(res => {
      console.log(res);
      const data = res.result;

      //获取轮播图的数据
      this.topImages = data.itemInfo.topImages;

      //整合获取商品信息数据
      this.goods = new Goods(
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );

      //获取店铺信息
      this.shop = new Shop(data.shopInfo);
      //保存商品的详情数据
      this.detailInfo = data.detailInfo;

      //获取产品参数信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );

      //取出评论的信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }

      //方法1 获取对应的offsetTop值，存在缺陷
      // this.$nextTick(() => {
      //   //根据最新的数据，对应的dom已经渲染出来，但图片没有获取到高度，导致高度不准确
      //   this.themeTopYs=[];
      //   this.themeTopYs.push(0);
      //   this.themeTopYs.push(this.$refs.params.$el.offsetTop);
      //   this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      //   this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
      //   console.log(this.themeTopYs)
      // })

      //请求推荐数据
      getRecommend().then(res => {
        console.log(res);
        this.recommends = res.data.list;
      });

      //方法3 获取对应的offsetTop值，采用变量赋值调防抖
      this.getThemeTopY = debounce(() => {
        this.themeTopYs = [];
        this.themeTopYs.push(0);
        this.themeTopYs.push(this.$refs.params.$el.offsetTop);
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
        this.themeTopYs.push(Number.MAX_VALUE);
        console.log(this.themeTopYs);
      }, 100);
    });
  },

  destroyed() {
    this.$bus.$off("homeitemImageLoad", this.itemImgListenter);
  },
  methods: {
    ...mapActions(["addCart"]),

    //监听底部图片是否已经加载完后实时刷新scroll内部高度
    detailImageLoad() {
      // this.$refs.scroll.refresh(); 没有防抖会平凡的被调用
      this.newrefresh();
      //二次调用取值
      this.getThemeTopY();
      //方法2 获取对应的offsetTop值，存在缺陷调用频繁
      // this.themeTopYs=[];
      // this.themeTopYs.push(0);
      // this.themeTopYs.push(this.$refs.params.$el.offsetTop);
      // this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      // this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
      // console.log(this.themeTopYs)
    },
    //顶部导航的联动效果
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200);
    },
    //回到顶部消失的方法
    contentScroll(position) {
      const positionY = -position.y;

      //顶部导航滚动的联动效果
      //positionY和主题值进行对比
      //[0,1000,2000,Number.Max_value]
      //positionY 在0和1000之间 index = 0
      //positionY 在1000和2000之间 index = 1
      //positionY 在2000和3000之间 index = 2
      //positionY 在3000和最大值之间 index = 3

      let length = this.themeTopYs.length;
      for (let i = 0; i < length - 1; i++) {
        if (
          this.currentIndex !== i &&
          positionY >= this.themeTopYs[i] &&
          positionY < this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }

      this.listenScroll(position);
    },
    addToCart() {
      const products = {};
      products.imgURL = this.topImages[0];
      products.title = this.goods.title;
      products.desc = this.goods.desc;
      products.newPrice = this.goods.nowPrice;
      products.iid = this.iid;
      //2-将商品添加到购物车里
      //第一种
      // this.$store.dispatch("addCart", products);
      //第二种
      this.addCart(products).then(res => {
        this.$toast.showToast(res);
      });
    }
  }
};
</script>

<style scoped>
#detail {
  position: relative;
  height: 100vh;
  background-color: #fff;
  z-index: 10;
  overflow-y: hidden;
}
.detail-nav {
  position: relative;
  z-index: 10;
  background-color: #fff;
}
.content {
  height: calc(100% - 44px - 49px);
}
</style>
